/**
 * 用户认证相关的工具函数
 * 用于管理用户登录状态、token存储和用户信息
 */

// 用户角色常量
export const UserRole = {
  USER: 0,    // 普通用户
  ADMIN: 1    // 管理员
} as const

// 权限常量
export const Permission = {
  THEME_SWITCH: 'theme_switch',           // 主题切换
  LANGUAGE_SWITCH: 'language_switch',     // 语言切换
  VOICE_CHAT: 'voice_chat',               // 语音对话
  ANIMATION_CONTROL: 'animation_control', // 数字人动画控制
  DIGITAL_HUMAN_VIEW: 'digital_human_view', // 数字人查看
  EXHIBITION_ACCESS: 'exhibition_access', // 3D展览馆访问
  VIDEO_WATCH: 'video_watch',             // 视频观看
  DANMAKU_CONTROL: 'danmaku_control'      // 弹幕控制
} as const

// 类型定义
export type UserRoleType = typeof UserRole[keyof typeof UserRole]
export type PermissionType = typeof Permission[keyof typeof Permission]

// 用户信息接口
export interface UserInfo {
  username: string
  role: number
  userId: number
  token: string
}

// 角色权限映射
const ROLE_PERMISSIONS: Record<UserRoleType, PermissionType[]> = {
  [UserRole.ADMIN]: [
    Permission.THEME_SWITCH,
    Permission.LANGUAGE_SWITCH,
    Permission.VOICE_CHAT,
    Permission.ANIMATION_CONTROL,
    Permission.DIGITAL_HUMAN_VIEW,
    Permission.EXHIBITION_ACCESS,
    Permission.VIDEO_WATCH,
    Permission.DANMAKU_CONTROL
  ],
  [UserRole.USER]: [
    Permission.DIGITAL_HUMAN_VIEW,
    Permission.EXHIBITION_ACCESS,
    Permission.VIDEO_WATCH
  ]
}

// 从localStorage获取用户信息
export const getUserInfo = (): UserInfo | null => {
  try {
    const userInfoStr = localStorage.getItem('userInfo')
    if (userInfoStr) {
      return JSON.parse(userInfoStr)
    }
    return null
  } catch (error) {
    console.error('获取用户信息失败:', error)
    return null
  }
}

// 保存用户信息到localStorage
export const setUserInfo = (userInfo: UserInfo): void => {
  try {
    localStorage.setItem('userInfo', JSON.stringify(userInfo))
    localStorage.setItem('token', userInfo.token)
  } catch (error) {
    console.error('保存用户信息失败:', error)
  }
}

// 获取token
export const getToken = (): string | null => {
  return localStorage.getItem('token')
}

// 清除用户信息
export const clearUserInfo = (): void => {
  localStorage.removeItem('userInfo')
  localStorage.removeItem('token')
}

// 检查用户是否已登录
export const isLoggedIn = (): boolean => {
  const token = getToken()
  const userInfo = getUserInfo()
  return !!(token && userInfo)
}

// 检查用户角色权限
export const hasRole = (requiredRole: number): boolean => {
  const userInfo = getUserInfo()
  return userInfo ? userInfo.role >= requiredRole : false
}

// 获取当前用户名
export const getCurrentUsername = (): string | null => {
  const userInfo = getUserInfo()
  return userInfo ? userInfo.username : null
}

// 检查用户是否有特定权限
export const hasPermission = (permission: PermissionType): boolean => {
  const userInfo = getUserInfo()
  if (!userInfo) return false
  
  const userRole = userInfo.role as UserRoleType
  const permissions = ROLE_PERMISSIONS[userRole] || []
  return permissions.includes(permission)
}

// 检查用户是否有任一权限
export const hasAnyPermission = (permissions: PermissionType[]): boolean => {
  return permissions.some(permission => hasPermission(permission))
}

// 检查用户是否有所有权限
export const hasAllPermissions = (permissions: PermissionType[]): boolean => {
  return permissions.every(permission => hasPermission(permission))
}

// 获取用户角色名称
export const getRoleName = (role: number): string => {
  switch (role) {
    case UserRole.ADMIN:
      return '管理员'
    case UserRole.USER:
      return '普通用户'
    default:
      return '未知角色'
  }
}

// 获取当前用户角色名称
export const getCurrentRoleName = (): string => {
  const userInfo = getUserInfo()
  return userInfo ? getRoleName(userInfo.role) : '未登录'
}

// 检查是否为管理员
export const isAdmin = (): boolean => {
  const userInfo = getUserInfo()
  return userInfo ? userInfo.role === UserRole.ADMIN : false
}

// 检查是否为普通用户
export const isUser = (): boolean => {
  const userInfo = getUserInfo()
  return userInfo ? userInfo.role === UserRole.USER : false
}
